﻿using System;
using System.Reflection;
using System.Collections.Generic;
using NLog;
using dlCentral.Utils;
using dlCentral.PluginHandlers;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Linq;

namespace dlCentral.TorrentHandlers {
    internal class CTHandler : TorrentHandler {
        private static Logger logger = LogManager.GetCurrentClassLogger();
        

        // The torrent ID for the plugin this handler implements support for.
        public override TorrentId ID
        {
            get { return TorrentId.CT; }
            set { }
        }

        // Should return the name of the source Torrent providing the data.
        public override string TorrentName {
            get { return "CinemaTorrents"; }
            set { }
        }

        public override void GetDetails(List<MediaDetail> Results)
        {
            url = "http://cinematorrents.com/api/search?&title={0}&year={1}&year_op=LT"; 
            JObject jobject = new JObject();
            string size = "";
            string seeds = "";
            string peers = "";
            string date = "";
            int year = DateTime.Now.Year;
            Year = year.ToString();
            url = string.Format(url, SearchTerm,Year); 
            string json = get(url);
            try
            {
                jobject = JObject.Parse(json);
            }
            catch
            {
                return;
            }

            JArray ja = (JArray)jobject.SelectToken("result");
            foreach (JObject jo in ja)
            {
                MediaDetail item = new MediaDetail();
                item.Name = (string)(jo.SelectToken("title"));
                item.TorrentURL = (string)(jo.SelectToken("download"));
                date = (string)(jo.SelectToken("added"));

//                item.TorrentURL.Replace("http://www.torrentz.com/", "");
//                item.TorrentURL = item.TorrentURL.ToUpper();

//                item.TorrentURL = "http://torcache.com/torrent/" + item.TorrentURL;
//                item.TorrentURL = item.TorrentURL + ".torrent";  
                size = (string)(jo.SelectToken("size"));
                try
                {
                    seeds = (string)(jo.SelectToken("seeds"));
                }
                catch { seeds = "0"; }
                try
                {
                    peers = (string)(jo.SelectToken("peers"));
                }
                catch { peers = "0"; }

                item.Date = DateTime.Parse(date);
                item.Plot = (string)(jo.SelectToken("imdb_plot"));
                item.Thumb = (string)(jo.SelectToken("cover_large"));
                item.IsInfoChecked = true;
                item.TorrentInfo = size + " S/P : " + seeds + "/" + peers;
                item.SearchType = DLSType;
                item.Site = Site;
                Results.Add(item);
            }
        }

        protected override bool IsAvailable()
        {
            return true;
        }


    }
}
